<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 300px;
            margin: 100px auto;
            text-align: center;
            border-collapse: collapse;
            border: 1px solid #ccc;

        }
        thead {
            height: 35px;
            background-color: skyblue;
        }
        tbody th {
            border: 1px solid #ccc;
            height: 35px;
            font-weight: 400;
            font-size: 14px;
        }
    </style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th><input type="checkbox"  id = 'j_cball'></th>
                <th>商品</th>
                <th>价钱</th>
            </tr>
        </thead>
        <tbody id="j_tb">
            <tr>
                <th>
                    <input type="checkbox">
                </th>
                <th>iphone8</th>
                <th>8000</th>
            </tr>
            <tr>
                <th>
                    <input type="checkbox">
                </th>
                <th>ipad pro</th>
                <th>10000</th>
            </tr>
            
            <tr>
                <th>
                    <input type="checkbox">
                </th>
                <th>iphone14</th>
                <th>15800</th>
            </tr>
            <tr>
                <th>
                    <input type="checkbox">
                </th>
                <th>xiaomi</th>
                <th>3000</th>
            </tr>
            
        </tbody>
    </table>
    <script>
        var j_cball = document.getElementById('j_cball');
        var j_tbs = document.getElementById('j_tb').getElementsByTagName('input');
        j_cball.onclick = function() {

            for (var i = 0; i < j_tbs.length; i++) {
                j_tbs[i].checked = this.checked;
            }
        }
        for ( var i = 0; i < j_tbs.length; i++) {
            j_tbs[i].onclick = function () {
                // flag控制全选按钮是否选中
                var flag = true;
                for ( var i = 0; i < j_tbs.length; i++) {
                    if (!j_tbs[i].checked ) {
                       flag = false;
                       break;//只要有一个没选中，退出循环，提高执行效率
                    }
                }
                j_cball.checked = flag;
            }
        }
    </script>
</body>
</html>